from heapq import heappop, heappush
from typing import *


class Solution:
    def minTimeToReach(self, moveTime: List[List[int]]) -> int:
        m, n = len(moveTime), len(moveTime[0])
        time = [[0] * n for _ in range(m)]
        q = [(0, 0, 0)]
        while q:
            t, x, y = heappop(q)
            for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
                if (
                    (tx := x + i) >= 0
                    and tx < m
                    and (ty := y + j) >= 0
                    and ty < n
                    and time[tx][ty] == 0
                ):
                    time[tx][ty] = (
                        t + 1 if t > moveTime[tx][ty] else moveTime[tx][ty] + 1
                    )
                    if tx == m - 1 and ty == n - 1:
                        return time[tx][ty]
                    heappush(q, (time[tx][ty], tx, ty))
